<?php

/**
 * @file
 *
 * @package    Europa
 * @subpackage View
 */

/**
 * @class
 *
 * @name Europa_View_Layou
 * @desc Renders view scripts. Used to render nested layouts.
 */
class Europa_View_Layout extends Europa_View
{
	protected $_subview;
	protected $_layoutview;
	protected $_config = array('viewPath' => './app/layouts', 'layoutDefault' => 'index');

	/**
	 * 
	 * @param $config
	 * @param $layoutview
	 * @param $subview
	 * @return unknown_type
	 */
	public function __construct($config, $layoutview, $subview)
	{
		parent::__construct($config);
		if($layoutview instanceof Europa_View)
			$this->_layoutview = $layoutview;
		else
		{
			$this->_layoutview = new Europa_View_Basic($this->getConfig());
			$this->_layoutview->render($this->getConfig('layoutDefault'));
		}
		if($subview instanceof Europa_View)
			$this->_subview = $subview;
		else
			$this->_subview = new Europa_View_Basic();
	}

	/**
	 * @method
	 * @magic
	 *
	 * @name __toString
	 * @desc Returns the contents of the rendered view.
	 *
	 * @return String
	 */
	public function __toString()
	{
		return $this->_layoutview->__toString();
	}

	/**
	 * @method
	 * @public
	 *
	 * @name render
	 * @desc Sets the script to be rendered.
	 *
	 * @param String $script - The path to the script to be rendered relative to the view path, excluding the extension.
	 *
	 * @return Object Europa_View
	 */
	public function render($script)
	{
		$this->layout->view = $this->_subview->render($script);
	}

	/**
	 * Use the magic getter to set view variables
	 * @method
	 * @magic
	 * @param string $name
	 * @return The view variable
	 */
	public function __get($name)
	{
		if($name === 'layout')
			return $this->_layoutview;
		else
			return $this->_subview->$name;
	}

	/**
	 * Use the magice setter for view variables
	 * @method
	 * @magic
	 * @param unknown_type $name The variable name
	 * @param unknown_type $value The variable value
	 * @return boolean True, if it could be set
	 */
	public function __set($name, $value)
	{
		return $this->_subview->$name = $value;
	}
}
